using System;
using Crainiate.Data.Persistence.Providers;

namespace Crainiate.Data.Persistence
{
    [Serializable]
	public abstract class View<T>: IPersistable, IView
    where T: class, IPersistable
	{
        //Property variables
        [NonSerialized] Command _command;
        private Concurrency _concurrency;

        #region Interface

        //Constructors
        public View()
        {
        }

        public View(Command command)
        {
            Command = command;
        }

        //Properties
        public virtual Command Command
        {
            get
            {
                return _command;
            }
            set
            {
                _command = value;
            }
        }

        public virtual Concurrency Concurrency
        {
            get
            {
                return _concurrency;
            }
            set
            {
                _concurrency = value;
            }
        }

        //Methods
        public Type UnderlyingType()
        {
            return typeof(T);
        }

        public virtual void Select()
        {
            if (Command == null) throw new ViewException("The view could not be selected. Set the Command before attempting to select this view.");
            Command.Select(this);
        }

        #endregion
	}
}
